/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rejeev.tracer;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;

/**
 *
 * @author rejeev.divakaran
 */
public class EntryExitTransformer implements ClassFileTransformer{
    //private Set<String> includeList;
    //private Set<String> excludeList;

    public byte[] transform(ClassLoader arg0, String arg1, Class<?> arg2, ProtectionDomain arg3, byte[] arg4) throws IllegalClassFormatException {
        //System.out.println("in transform: " + arg1);
        try{
            //System.out.println("Transforming class: " + arg1);
            ClassReader cr = new ClassReader(arg4);
            ClassWriter cw = new ClassWriter(cr, 0);
            TracerClassAdapter ca = new TracerClassAdapter(cw);
            //if(includeList != null) ca.setExcludeList(excludeList);
            //if(excludeList != null) ca.setIncludeList(includeList);
            cr.accept(ca, ClassReader.EXPAND_FRAMES);
            return cw.toByteArray();
        }catch(Exception e){
            e.printStackTrace();
        }
        return arg4;
    }
    /*
    public void setExcludeList(Set<String> filter){
        excludeList = filter;
    }
    
    public void setIncludeList(Set<String> filter){
        includeList = filter;
    }
    */
}
